//
//  OpenLotteryRecordCell.m
//  WanCaiC8
//
//  Created by tangang on 2018/4/24.
//  Copyright © 2018年 tangang. All rights reserved.
//

#import "OpenLotteryRecordCell.h"
#import "LotteryNumCell.h"
#import "LabCell.h"
@interface OpenLotteryRecordCell()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (nonatomic ,strong) UICollectionView *collectionView;
@property (nonatomic ,strong) UICollectionView *bottomCollectionView;
@end

@implementation OpenLotteryRecordCell{
    QXLable     *issueLabTitle;
    QXLable     *timeLabTitle;
    NSArray     *arrData;
    NSArray     *arrAlias;
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        UIView *labelView=[[UIView alloc]init];
        labelView.backgroundColor=[UIColor whiteColor];
        [self.contentView addSubview:labelView];
        [labelView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.equalTo(self.contentView);
            make.height.mas_equalTo(40);
        }];
        
        issueLabTitle=[[QXLable alloc]initTitle];
        [labelView addSubview:issueLabTitle];
        [issueLabTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(labelView).with.offset(10);
            make.centerY.equalTo(labelView);
        }];
        
        timeLabTitle=[[QXLable alloc]initTitle];
        timeLabTitle.font = [UIFont systemFontOfSize:14];
        timeLabTitle.textAlignment = NSTextAlignmentRight;
        [labelView addSubview:timeLabTitle];
        [timeLabTitle mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(labelView).with.offset(-10);
            make.centerY.equalTo(labelView);
        }];
        
        
        UIView *line1=[[UIView alloc]init];
        line1.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:line1];
        [line1 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(0.7);
            make.top.equalTo(labelView.mas_bottom);
        }];
        
        
        
        [self.contentView addSubview:self.collectionView];
        [self.collectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(issueLabTitle);
            make.top.equalTo(line1.mas_bottom).with.offset(5);
            make.right.equalTo(self.contentView);
            make.height.mas_equalTo(40);
        }];
        
        [self.contentView addSubview:self.bottomCollectionView];
        [self.bottomCollectionView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(issueLabTitle);
            make.top.equalTo(self.collectionView.mas_bottom);
            make.right.equalTo(self.contentView);
            make.height.mas_equalTo(40);
            make.bottom.equalTo(self.contentView).with.offset(-5);
        }];
        
        UIView *line2=[[UIView alloc]init];
        line2.backgroundColor=DEFAULTBGCOLOR;
        [self.contentView addSubview:line2];
        [line2 mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(self.contentView);
            make.height.mas_equalTo(0.7);
            make.bottom.equalTo(self.contentView);
        }];
    }
    return self;
}

- (void)setModel:(LotteryRecordModel *)model{
    _model=model;
    
    
    NSDictionary *titleDict1 = @{NSFontAttributeName: [UIFont systemFontOfSize:14],
                                NSForegroundColorAttributeName: [UIColor redColor]};
    NSDictionary *titleDict2 = @{NSFontAttributeName: [UIFont systemFontOfSize:14]};
   NSAttributedString *issueStr1 = [[NSAttributedString alloc] initWithString:model.Issue attributes:titleDict1];
    NSAttributedString *issueStr2 = [[NSAttributedString alloc] initWithString:@"期" attributes:titleDict2];
    
    NSMutableAttributedString *attributedString = [[NSMutableAttributedString alloc] initWithAttributedString:issueStr1];
    [attributedString appendAttributedString:issueStr2];

    issueLabTitle.attributedText=attributedString;
    
    
    timeLabTitle.text=[NSString stringWithFormat:@"%@",model.SubTime];
    if (model.lType==5) {
        NSMutableArray *arr=[[NSMutableArray alloc]init];
        [arr addObjectsFromArray:model.numArr];
        [arr insertObject:@"+" atIndex:arr.count-1];
        arrData=arr;
        NSMutableArray *arr1=[[NSMutableArray alloc]init];
        [arr1 addObjectsFromArray:model.infoArr];
        [arr1 insertObject:@"+" atIndex:arr1.count-1];
        arrAlias=arr1;
        [self.bottomCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(30);
            make.bottom.equalTo(self.contentView).with.offset(-5);
        }];
        [self.bottomCollectionView reloadData];
    }else{
        if (model.lType==65) {
            arrData=model.PCArr;
        }else{
            arrData=model.numArr;
        }
        [self.bottomCollectionView mas_updateConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(@0);
            make.bottom.equalTo(self.contentView);
        }];
    }
    [self.collectionView reloadData];
}

#pragma mark - collectionView代理函数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (collectionView==self.bottomCollectionView) {
        return arrAlias.count;
    }
    return arrData.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    LotteryNumCell *cell=[collectionView dequeueReusableCellWithReuseIdentifier:@"LotteryNumCell" forIndexPath:indexPath];
    if (collectionView==self.bottomCollectionView) {
        [cell setDownTitle:arrAlias[indexPath.row] andType:self.lotteryType];
    }else{
        
        [cell setTitle:arrData[indexPath.row] andType:self.lotteryType];
        
        if ((self.lotteryType==LotteryNumTypeChromosphere||self.lotteryType==LotteryNumTypeBrings||self.lotteryType==LotteryNumTypeSevenLecai)&&(indexPath.row==arrData.count-1)) {
            [cell setBGColor:DEFAULTTEXTBLUECOLOR];
        }else if (self.lotteryType==LotteryNumTypeBrings&&(indexPath.row==arrData.count-2)){
            [cell setBGColor:DEFAULTTEXTBLUECOLOR];
        }
        
    }
    
    return cell;
}

-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    return UIEdgeInsetsMake(0, 3, 0, 3);
}

- (UICollectionView *)collectionView{
    if (_collectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(30, 30)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.backgroundColor=[UIColor whiteColor];
        _collectionView.showsVerticalScrollIndicator=NO;
        [_collectionView registerClass:[LotteryNumCell class] forCellWithReuseIdentifier:@"LotteryNumCell"];
    }
    return _collectionView;
}

- (UICollectionView *)bottomCollectionView{
    if (_bottomCollectionView==nil) {
        UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
        [layout setItemSize:CGSizeMake(30, 30)];
        [layout setScrollDirection:UICollectionViewScrollDirectionVertical];
        layout.minimumInteritemSpacing = 0.0;
        layout.minimumLineSpacing = 0.0;
        _bottomCollectionView = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:layout];
        _bottomCollectionView.delegate = self;
        _bottomCollectionView.dataSource = self;
        _bottomCollectionView.backgroundColor=[UIColor whiteColor];
        _bottomCollectionView.showsVerticalScrollIndicator=NO;
        [_bottomCollectionView registerClass:[LotteryNumCell class] forCellWithReuseIdentifier:@"LotteryNumCell"];
    }
    return _bottomCollectionView;
}

@end
